﻿using UnityEngine;
using Flower;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.Collections;

// IntroSceneController.cs
public class FirstIntroSceneController : MonoBehaviour
{
    private FlowerSystem fs;
    [SerializeField] private Animator animator;
    public float animationDuration = 1.0f;

    private void Start()
    {
        PatientManager patientManager = PatientManager.Instance;
        PatientCount patientCount = PatientCount.Instance;
        StartDialogue(patientManager.patients[patientCount.patientIndex].dialogueFile);
        fs.SetupUIStage("default", "DefaultUIStagePrefab", 10);
        for (int i = 0; i < 5; i++)
        {
            PatientManager.Instance.patients[i].gameObject.SetActive(false);

        }
        PatientManager.Instance.patients[patientCount.patientIndex].gameObject.SetActive(true);
        fs.RegisterCommand("show_buttons", parameters =>
        {
            fs.SetupButtonGroup();

            fs.SetupButton("开始战斗", () =>
            {
                // 按钮点击后触发场景切换
                StartCoroutine(LoadSceneAfterAnimation(parameters[0]));

                fs.Resume();
                fs.RemoveButtonGroup();
            });

            fs.Stop(); // 显示按钮后暂停系统
        });

    }
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            fs.Next();
        }
    }

    private void StartDialogue(string dialogueFile)
    {
        fs = FlowerManager.Instance.CreateFlowerSystem("default_first", true);
        fs.SetupDialog();
        fs.ReadTextFromResource(dialogueFile);
    }
    private IEnumerator LoadSceneAfterAnimation(string sceneName)
    {


        // 销毁 FlowerSystem 并切换场景
        if (fs != null) Destroy(fs);

        // 获取当前活跃场景
        Scene currentActiveScene = SceneManager.GetActiveScene();
        AsyncOperation ao = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); // 使用 Additive 模式加载新场景
        Scene newScene = SceneManager.GetSceneByName(sceneName);
        yield return ao;
        // 设置新加载的场景为活跃场景
        SceneManager.SetActiveScene(newScene);
        // 卸载原来的活跃场景
        SceneManager.UnloadSceneAsync(currentActiveScene);
    }


}
